package com.wf.smartcomm.server.admin.action;

import java.util.HashMap;


import javax.annotation.Resource;

import org.json.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.wf.smartcomm.server.bean.House;
import com.wf.smartcomm.server.bean.Payment;
import com.wf.smartcomm.server.bean.User;

import com.wf.smartcomm.server.service.PaymentService;
import com.wf.smartcomm.server.util.StringUtil;

@Controller
@Scope("prototype")
public class PaymentAction extends BaseAction implements ModelDriven<Payment>{

	private Payment payment = new Payment();
	@Resource
	private PaymentService paymentServiceImpl;
	
	public void add() throws Exception{
		
		if (payment.getType() == 4) {
			//其他费用,暂不处理
			printJsonData(200);
			return;
		}
		
		String hid = request.getParameter("hid");
		House house = new House();
		house.setObjectId(hid);
		payment.setHouse(house);
		
		if(!StringUtil.isEmpty(payment.getObjectId())){
			paymentServiceImpl.update(payment);
			session.removeAttribute("payment");
		}else {
			paymentServiceImpl.save(payment);
		}

		printJsonData(200);
	}
	
	public void list() throws Exception{
		String aoData = request.getParameter("aoData");
		String sEcho = "";// 记录操作的次数  每次加1
		String iDisplayStart = "";// 起始
		String iDisplayLength = "";// size
		String sSearch = "";// 搜索的关键字
		
		JSONObject jo = new JSONObject(aoData);
		sEcho = jo.getString("sEcho");
		iDisplayStart = jo.getString("iDisplayStart");
		iDisplayLength = jo.getString("iDisplayLength");
		sSearch = jo.getString("sSearch");
		page.setBeginIndex(Integer.parseInt(iDisplayStart));
		page.setPageSize(Integer.parseInt(iDisplayLength));
		User manager = (User) session.getAttribute("loginUser");
		if (!StringUtil.isEmpty(sSearch)) {
			paymentServiceImpl.queryByKey(manager.getLoginVillage(),sSearch,page);
		}else {
			paymentServiceImpl.list(manager.getLoginVillage(),page);
		}
		
		int  initEcho = Integer.parseInt(sEcho)+1;
		int count = page.getTotalCount();
		HashMap<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("iTotalRecords", count);
		dataMap.put("sEcho",initEcho);
		dataMap.put("iTotalDisplayRecords", count);
		dataMap.put("aaData", page.getDataList());
		printJsonData(outGson.toJson(dataMap));
	}
	
	public String edit() throws Exception{
		payment = paymentServiceImpl.get(payment.getObjectId());
		request.setAttribute("payment", payment);
		return "edit";
	}
	
	public void delete() throws Exception{
		paymentServiceImpl.delete(payment);
		printJsonData(200);
	}
	
	@Override
	public Payment getModel() {
		// TODO Auto-generated method stub
		return payment;
	}

}
